package com.yzh.pleaSys.controller;


import com.yzh.pleaSys.pojo.UserLogin;
import com.yzh.pleaSys.service.UserLoginService;
import com.yzh.pleaSys.utils.CodeUtil;
import com.yzh.pleaSys.utils.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import static com.yzh.pleaSys.entity.DataDict.USER_INFO;

@RestController
@RequestMapping("/userlogin")
public class UserLoginController {

    private final UserLoginService userLoginService;

    public UserLoginController(UserLoginService userLoginService) {
        this.userLoginService = userLoginService;
    }

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 根据用户输入的账号和密码进行登录验证
     *
     * @param username
     * @param password
     * @return
     */
    @GetMapping("/login")
    @ResponseBody
    public String findUserLogin(@RequestParam String username,
                                @RequestParam String password) {
        UserLogin userLogin = new UserLogin().builder()
                .username(username)
                .password(password)
                .build();

        if (null != this.userLoginService.findUserLogin(userLogin)) {
            //已登录的用户 将其信息存放于redis
            stringRedisTemplate.opsForHash().put(USER_INFO+":"+username,userLogin,"");
//            stringRedisTemplate.opsForHash().
            return "登陆成功！";
        }
        return null == this.userLoginService.findUserLogin(userLogin) ? "登陆失败！" : "登陆成功！";
    }

    /**
     * 注册用户
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    public String registerUserLogin(@RequestParam String username,
                                    @RequestParam String password) {
        UserLogin userLogin = new UserLogin().builder()
                .username(username)
                .password(password)
                .build();
        if (null != this.userLoginService.findUser(userLogin)) {
            return "该用户已注册";
        }
        return this.userLoginService.registerUserLogin(userLogin) ? "注册成功！" : "注册失败！";
    }

    @PostMapping("/bingemail")
    public String bindEmail() {
        return null;
    }

    /**
     * 更改密码
     *
     * @param userLogin
     * @param newPwd    新密码
     * @return
     */
    @PutMapping("/changepwd")
    @ResponseBody
    public String changePassword(@RequestBody UserLogin userLogin,
                                 @RequestParam String newPwd) {
//        UserLogin userLogin = new UserLogin().builder()
//                .id(9)
//                .username(username)
//                .password(password)
//                .build();
        if (null == this.userLoginService.findUserLogin(userLogin)) {
            return "您输入的账户或密码不对！";
        }
        return this.userLoginService.updatePwd(userLogin.getId(), newPwd) ? "修改密码成功！" : "修改密码失败！";
    }

    /**
     * 发送邮箱验证码用于密码修改
     *
     * @param email 目标邮箱
     * @return
     */
    @PutMapping("/sendcode")
    public String sendCode(@RequestParam String email) {

        String numCode = CodeUtil.getNumCode();

        return EmailUtil.sendCode(email, numCode) ? "验证码已发送！" : "验证码发送失败！";

    }

    /**
     * 忘记密码
     *
     * @param username
     * @param code     验证码
     * @return
     */
    @PutMapping("/resetpwd")
    @ResponseBody
    public String resetPassword(@RequestParam String username,
                                @RequestParam String code) {
        return null;
    }
}
